package com.oa.ylms.apps.business.assess.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.oa.ylms.apps.business.assess.IAssessInfoService;
import com.oa.ylms.apps.business.assess.IAssessModelService;
import com.oa.ylms.apps.business.plan.ICheckPlanService;
import com.oa.ylms.apps.common.DateUtil;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.constants.IPlanCategory;
import com.oa.ylms.apps.constants.assess.IAssessInfoStates;
import com.oa.ylms.apps.dao.assess.IAssessInfoDao;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.assess.AssessInfoValue;
import com.oa.ylms.apps.entity.assess.AssessModelValue;
import com.oa.ylms.apps.entity.plan.CheckPlanValue;

@SuppressWarnings("unchecked")
public class AssessInfoServiceImpl implements IAssessInfoService {

	private AssessInfoValue assessInfo;
	private IAssessInfoDao dao;
	private IAssessModelService assessModelService;
	private ICheckPlanService checkPlanService;
	private AssessModelValue assessModel;

	public int addAssessInfo(StaffInfoValue staffInfo, int yearNo, int monthNo, String assessName, int modelId) throws Exception {
		assessInfo.setYearNo(yearNo);
		assessInfo.setMonthNo(monthNo);
		assessInfo.setAssessName(assessName);
		assessInfo.setCreateTime(DateUtil.toDate(DateUtil.getCurrentDate()));
		int postId = staffInfo.getArchiveInfo().getPostInfo().getPostId();
		List<CheckPlanValue> list = checkPlanService.queryCheckPlanWithPage(
				IPlanCategory.ASSESSCHECK, postId, 10, 1);
		assessInfo.setCheckPlan(list.get(0));
		assessInfo.setAssessModel(assessModelService
				.queryAssessModelByModelId(modelId));
		assessInfo.setStates(IAssessInfoStates.SAVED);
	 return dao.addAssessInfo(assessInfo);

	}

	public String deleteAssessInfo(String assessIds) throws Exception {
		String unDelIds = "";
		String[] arrayAssessId = assessIds.split(",");
		for (String assessIdStr : arrayAssessId) {
			int assessId = Integer.parseInt(assessIdStr);
			assessInfo = queryAssessInfoByAssessId(assessId);
			assessInfo.setStates(IDEDStates.DELETE);
			boolean result = dao.editAssessInfo(assessInfo);
			if (result == false) {
				unDelIds = assessId + ",";
				continue;
			}
		}
		return unDelIds;
	}

	public boolean deleteAssessInfo(int assessId) throws Exception {
		assessInfo = dao.queryAssessInfoByAssessId(assessId);
		assessInfo.setStates(IDEDStates.DELETE);
		return dao.editAssessInfo(assessInfo);

	}

	public boolean modifyAssessInfo(StaffInfoValue staffInfo,int assessId)
			throws Exception {
		assessInfo = queryAssessInfoByAssessId(assessId);
		CheckPlanValue plan= assessInfo.getCheckPlan();
		String[] deptNo=null;
		if(plan!=null){
		   deptNo=plan.getPlanOrders().split(",");
		}
		assessInfo.setStates(Integer.parseInt(deptNo[0]));
		return dao.editAssessInfo(assessInfo);
	}

	public AssessInfoValue queryAssessInfoByAssessId(int assessId)
			throws Exception {
		return dao.queryAssessInfoByAssessId(assessId);
	}

	public int countTpages(int states, int yearNo, int monthNo,
			String modelName, String assessName, int rows) throws Exception {
		if (StringUtils.isNotEmpty(modelName)
				&& StringUtils.isNotBlank(modelName)) {
			assessModel.setModelName(modelName);
			assessModel.setStates(IAssessInfoStates.UNDEFINED);
		}
		if (StringUtils.isNotEmpty(assessName)
				&& StringUtils.isNotBlank(assessName)) {
			assessInfo.setAssessName(assessName);
			assessInfo.setStates(IAssessInfoStates.UNDEFINED);
		}
		return dao.countTpages(states, yearNo, monthNo, assessModel,
				assessInfo, rows);
	}

	public List<AssessInfoValue> queryAssessInfoWithPageByStates(int states,
			int yearNo, int monthNo, String modelName, String assessName,
			int rows, int pageNo) throws Exception {
		if (StringUtils.isNotEmpty(modelName)
				&& StringUtils.isNotBlank(modelName)) {
			assessModel.setModelName(modelName);
			assessModel.setStates(IAssessInfoStates.UNDEFINED);
		}
		if (StringUtils.isNotEmpty(assessName)
				&& StringUtils.isNotBlank(assessName)) {
			assessInfo.setAssessName(assessName);
			assessInfo.setStates(IAssessInfoStates.UNDEFINED);
		}
		return dao.queryAssessInfoWithPageByStates(states, yearNo, monthNo,
				assessModel, assessInfo, rows, pageNo);
	}

	public int getTotalNumber() throws Exception {
		return dao.getTotalNumber();
	}

	public AssessInfoValue getAssessInfo() {
		return assessInfo;
	}

	public void setAssessInfo(AssessInfoValue assessInfo) {
		this.assessInfo = assessInfo;
	}

	public IAssessInfoDao getDao() {
		return dao;
	}

	public void setDao(IAssessInfoDao dao) {
		this.dao = dao;
	}

	public IAssessModelService getAssessModelService() {
		return assessModelService;
	}

	public void setAssessModelService(IAssessModelService assessModelService) {
		this.assessModelService = assessModelService;
	}

	public AssessModelValue getAssessModel() {
		return assessModel;
	}

	public void setAssessModel(AssessModelValue assessModel) {
		this.assessModel = assessModel;
	}

	public List<Integer> getListMonth() throws Exception {
		List<Integer> listMonth = new ArrayList();
		for (int i = 1; i < 13; i++) {
			listMonth.add(i);
		}
		return listMonth;
	}

	public List<Integer> getListYear() throws Exception {
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		List<Integer> listYear = new ArrayList();
		for (int i = year; i >2004; i--) {
			listYear.add(i);
		}
		return listYear;
	}

	public ICheckPlanService getCheckPlanService() {
		return checkPlanService;
	}

	public void setCheckPlanService(ICheckPlanService checkPlanService) {
		this.checkPlanService = checkPlanService;
	}

}
